<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

// require_once '../app/libary/captcha/Code.class.php';

class IndexController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('backstage', ['except' => 'logout']);
    // }

    public function index(){
        // dd(555);
        $menu = BackstageModel::getMenu(['pid'=>0]);
        // dd(session('admin'));
        $info = CommonModel::getOne('admin_group',['id'=>session('admin')->id]);
        // dd(session('admin'));
        // 权限控制
        if(!empty($info->authority)){
            $permission=unserialize($info->authority);
        }
        $list=array();
        // dd($this->has_son(2));
        foreach ($menu as $v) {
            if($this->has_son($v->id)==true){
                $list[]=$v;
            }
        }
        // dd($list);
        return view('backstage/index/index',['menu'=>$list]);
    }


    public function info(){
        return view('backstage/index/info');
    }
    // 其他组件
    public function element(){
        return view('backstage/common/element');
    }

    public function getMenu(Request $request){
        if(!empty(session('admin'))){
            if( $request->isMethod('post') ){
                $data =$request->all();
                $arr = $this->menu_list($data['id']);
                // return $arr;
                // dd($arr);
                return response()->json(['res'=>'1','msg'=>'切换菜单！','data'=>$arr]);
            }
        }else{
            return response()->json(['res'=>'0','msg'=>'登录过期失效，请重新登录！','url'=>'http://'.$_SERVER['HTTP_HOST'].'/backstage/login']);
        }
    }

    private function menu_list($pid =0,$rs = array()){

        $arr1 = BackstageModel::getMenu(['pid'=>intval($pid)]);

        $info = $rs;

        foreach ($arr1 as $key => $val) {
            // if($this->has_son($val->id)==true){
                $rs[] = array(
                            'p'=>$val,
                            'c'=>$this->menu_list($val->id,$info)
                        );
            // }
        }
            
        return $rs;
    }
    public function has_son($id=0){
        $arr1 = BackstageModel::getMenu(['pid'=>$id]);
        foreach ($arr1 as $val) {

            if($val->level==2){
                $info = CommonModel::getOne('admin_group',['id'=>session('admin')->id]);
                // 权限控制
                $permission=unserialize($info->authority);
                if(!empty($permission[$val->action])){
                    return true;
                }
            }else{
                return $this->has_son($val->id);
            }
        }
        return false;
    }





}
